
var Dom, Event, Connect;
var ua = navigator.userAgent.toLowerCase();
var isIE = ua.indexOf("msie") > -1, isIE7 = ua.indexOf("msie 7") > -1, isSafari = (/webkit|khtml/).test(ua), isGecko = !isSafari && ua.indexOf("gecko") > -1;
if (isIE && !isIE7) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	}
	catch (A) {
	}
}
if (typeof YAHOO == "undefined") {
	window.onload = function () {
		var A = document.getElementById("loading_info");
		if (A) {
			A.innerHTML = "出现异常：缺少YUI的核心类，请确认是否正确加载了YUI的utilities文件！";
		}
	};
} else {
	Dom = YAHOO.util.Dom;
	Event = YAHOO.util.Event;
	Connect = YAHOO.util.Connect;
}
var PortalCore = function () {
	this.ddm = null;
	this.columns = null;
	this.modules = null;
};
PortalCore.prototype = {init:function (sCookie) {
	this.clear();//先对各模板的内容进行清理（保存到一个隐藏panel中），否则进行动态添加时，原来panel中的内容会被删除掉，导致重新加载时找不到内容）
	this.showInfo(sCookie);
	this.ddm = new DDModules();
	this.initColumns(sCookie);
}, initColumns:function (sCookie) {
	this.columns = sCookie.split("|");
	this.initModules(this.columns);
	this.crtContainer();
}, initModules:function (columns) {
	this.modules = [];
	for (var i = 0, length = columns.length; i < length; i++) {
		var column = columns[i].split(",");
		for (var j = 0, cLength = column.length; j < cLength; j++) {
			var module = column[j];
			if (module) {
				this.modules.push(module);
			}
		}
	}
}, getRow:function () {
	var container = Dom.get("main_container");
	if (container) {
		return container.rows[0];
	}
}, getCells:function () {
	var row = this.getRow();
	if (row) {
		return row.cells;
	}
}, adjustWidth:function () {
	var cells = this.getCells();
	var percent = parseInt(100 / cells.length) + "%";
	Dom.setStyle(cells, "width", percent);
	Dom.setStyle(cells[0], "border", "0px");
}, adjustView:function () {
	var I = this.getListLength();
	if (I) {
		for (var G = 0, F = this.modules.length; G < F; G++) {
			var D = this.modules[G];
			var H = Dom.get(D + "_drag");
			if (H) {
				var J = H.title;
				if (J) {
					H.firstChild.nodeValue = J.ellipse(I.titleLen);
				}
			}
			var K = Dom.get(D + "_content");
			if (K) {
				var E = K.getElementsByTagName("a");
				for (var L = 0, A = E.length; L < A; L++) {
					var B = E[L];
					if (B) {
						var C = B.title;
						if (C) {
							B.firstChild.nodeValue = C.ellipse(I.listLen);
						}
					}
				}
			}
		}
	}
}, crtContainer:function () {
	var mainArea = Dom.get(PanelInfo.mainArea);
	if (mainArea) {
		mainArea.innerHTML = this.tplContainer();
		this.adjustWidth();
		this.crtModules();
	}
}, tplContainer:function () {
	var column = [];
	column[0] = "<table id=\"main_container\" class=\"wFull\"><tr valign=\"top\">";
	for (var i = 0, length = this.columns.length; i < length; i++) {
		column[column.length] = "<td id=\"col" + (i + 1) + "\" class=\"cell\"></td>";
	}
	column[column.length] = "</tr></table>";
	return column.join("");
}, crtModules:function () {
	var cells = this.getCells();
	for (var i = 0, length = cells.length; i < length; i++) {
		cells[i].innerHTML = this.tplModule(i);
	}
	this.enableDDM(cells);
	this.fillContent();
	this.addHandler();
}, tplModule:function (index) {
	var content = [];
	var modules = this.columns[index].split(",");
	for (var i = 0, length = modules.length; i < length; i++) {
		var module = modules[i];
		if (module == "") {
			continue;
		}
		var moduleInfo = PanelInfo.rssInfo[module];
		if (moduleInfo) {
			var J = this.getListLength();
			var K = moduleInfo.title;
			if(K == null)
				K = 'panel title';
			var A = K;
			if (J) {
				A = K.ellipse(J.titleLen);
			}

			content[content.length] = "<div id=\"" + module + "\" class=\"cB p2\">";
			content[content.length] = "<table class=\"cB title wFull\"><tr>";
			content[content.length] = "<td id=\"" + module + "_drag\" class=\"p3\" title=\"" + K + "\">" + A + "</td>";
			
			if(moduleInfo.contentDetail)
				content[content.length] = "<td class=\"w1\"><a id=\"" + module + "_more\" class=\"btnMore\" href=\"" + moduleInfo.contentDetail + "\" target=\"_blank\" title=\"\u66F4\u591A\"><span id=\"" + module + "_more\" class=\"fL btnMore\" title=\"\u66F4\u591A\"></span></a></td>";
			content[content.length] = "<td class=\"w1\"><span id=\"" + module + "_min\" class=\"fL btnMin\" title=\"\u7f29\u653e\"></span></td>";
			content[content.length] = "<td class=\"w1\"><span id=\"" + module + "_close\" class=\"fL btnClose\" title=\"\u5173\u95ed\"></span></td>";
			content[content.length] = "</tr></table>";
			content[content.length] = "<div id=\"" + module + "_sub\" class=\"cB\">";
			content[content.length] = "<div id=\"" + module + "_content\" class=\"cB mContent\"></div>";
			content[content.length] = "</div></div>";
		}
	}
	content[content.length] = "<p class=\"cB mSpace\"></p>";
	return content.join("");
}, enableDDM:function (A) {
	this.ddm.av = false;
	if (!A) {
		A = this.getCells();
	}
	this.ddm.aD(A);
}, saveSetting:function () {
	var E = [], K = this.getCells();
	for (var C = 0, I = K.length; C < I; C++) {
		var D = K[C].childNodes;
		if (D.length > 1) {
			for (var G = 0, A = D.length; G < A; G++) {
				var H = D[G].id;
				if (H) {
					var B = (G == A - 2) ? "|" : ",";
					E[E.length] = H + B;
				}
			}
		} else {
			E[E.length] = "|";
		}
	}
	var J = E.join("");
	var F = J.substring(0, J.length - 1);
	this.columns = F.split("|");
	this.initModules(this.columns);
	this.hideEl("notice_area");
	if(PanelInfo.id == null)
		this.setCookie("modules", F);
	else
		this.setCookie(PanelInfo.id+"_modules", F);
	this.showInfo(F);
}, getListLength:function () {
	var A, B = this.columns.length;
	switch (B) {
	  case 6:
		A = {listLen:15, titleLen:11};
		break;
	  case 5:
		A = {listLen:20, titleLen:15};
		break;
	  case 4:
		A = {listLen:32, titleLen:25};
		break;
	  case 2:
		A = {listLen:62, titleLen:45};
		break;
	  case 1:
		A = {listLen:80, titleLen:55};
		break;
	  default:
		A = {listLen:46, titleLen:35};
		break;
	}
	return A;
}, processXML:function (A, E, D) {
	var C = Dom.get(E + "_content");
	Dom.setStyle(C, "height", "100px");
	C.innerHTML = "<img src=\"${path}/themes/portlet/img/ico_loading.gif\" alt=\"loading\" align=\"absmiddle\" /> \u6b63\u5728\u8f7d\u5165\u6a21\u5757\u5185\u5bb9...";
	var G = this;
	var B = function (A) {
		var L = [], N = G.getListLength();
		L[0] = "<ul class=\"cB\">";
		var M = A.responseXML.getElementsByTagName("item");
		var H = M.length;
		var B = (D < H) ? D : H;
		for (var I = 0; I < B; I++) {
			var E = M[I];
			var P = E.getElementsByTagName("link");
			var F = E.getElementsByTagName("title");
			var J = G.getXMLNode(P[0].firstChild);
			var O = G.getXMLNode(F[0].firstChild);
			var K = O;
			if (N) {
				K = O.ellipse(N.listLen);
			}
			L[L.length] = "<li><a href=\"" + J + "\" title=\"" + O + "\" target=\"_blank\">" + K + "</a></li>";
		}
		L[L.length] = "</ul>";
		if (C) {
			Dom.setStyle(C, "height", "auto");
			C.innerHTML = L.join("");
		}
	};
	var F = function (A) {
		if (C) {
			C.innerHTML = "\u5f02\u6b65\u8bfb\u53d6\u6570\u636e\u51fa\u9519\uff0c\u8bf7\u7a0d\u540e\u5237\u65b0\u91cd\u8bd5\uff01";
		}
	};
	Connect.asyncRequest("POST", A, {success:B, failure:F}, "");
}, getXMLNode:function (A) {
	if (A) {
		return A.nodeValue.replace(/\</, "&lt;").replace(/\>/, "&gt;");
	} else {
		return "";
	}
}, addHandler:function () {
	var editLength = this.editLength;
	for (var i = 0, E = this.modules.length; i < E; i++) {
		var module = this.modules[i];
		if (module) {
			this.onHandler(module);
		}
	}
}, onHandler:function (G) {
	var E = G + "_drag";
	var J = G + "_close";
	var D = G + "_min";
	var A = G + "_select";
	Dom.setStyle(E, "cursor", "move");
	Dom.setStyle([J, D], "cursor", "pointer");
	var H = Dom.get(A);
	if (H) {
		var F = this.editLength;
		H[F].selected = true;
	}
	var C = function () {
		this.confirmClose(G);
	};
	Event.on(J, "click", C, this, true);
	C = function () {
		this.swapDisplay(G + "_sub");
	};
	Event.on(D, "click", C, this, true);
	C = function () {
		this.swapDisplay(G + "_edit_panel");
	};
	//Event.on(B, "click", C, this, true);
	C = function () {
		if (H) {
			var A = this.getProcessUrl(G);
			var B = parseInt(H[H.selectedIndex].value);
			this.hideEl(G + "_edit_panel");
			this.processXML(A, G, B);
		}
	};
	Event.on(A, "change", C, this, true);
	//Event.on(I, "click", C, this, true);
}, hideEl:function (A) {
	Dom.setStyle(A, "display", "none");
}, confirmClose:function (B) {
	var C = PanelInfo.rssInfo[B].title;
	C = C ? "\u201c" + C + "\u201d" : "\u8fd9\u4e2a";
	var A = window.confirm("\u60a8\u786e\u5b9a\u8981\u5173\u95ed" + C + "\u7248\u5757\u5417\uff1f");
	if (A) {
		this.hideEl(B);
	}
}, swapDisplay:function (A) {
	var B = Dom.getStyle(A, "display");
	if (B == "none") {
		Dom.setStyle(A, "display", "block");
	} else {
		Dom.setStyle(A, "display", "none");
	}
}, crtCell:function (C) {
	var A = this.getRow();
	if (A) {
		var B = Dom.get("col" + C);
		if (!B) {
			B = document.createElement("td");
			B.id = "col" + C;
			B.className = "cell";
			A.appendChild(B);
			B.innerHTML = "<p class=\"cB mSpace\"></p>";
		}
	}
}, delCell:function (B) {
	var A = this.getRow();
	if (A) {
		var C = Dom.get("col" + B);
		if (C) {
			A.removeChild(C);
		}
	}
}, crtColumn:function (A) {
	this.crtCell(A);
}, delColumn:function (J, F) {
	var K = Dom.get("col" + F);
	if (K) {
		var B = K.childNodes;
		for (var H = 0, E = B.length; H < E; H++) {
			var G = B[H];
			if (G.tagName.toLowerCase() == "p") {
				K.removeChild(G);
			}
		}
		var L = Dom.get("col" + J);
		if (L) {
			var C = L.childNodes;
			for (var I = 0, A = C.length; I < A; I++) {
				var D = C[I].cloneNode(true);
				K.appendChild(D);
			}
		}
		this.delCell(J);
	}
}, setColumn:function (B) {
	var C = this.getCells();
	var A = C.length;
	switch (B) {
	  case 6:
		if (A == 6) {
			return;
		}
		if (A == 5) {
			this.crtColumn(6);
		}
		if (A == 4) {
			this.crtColumn(5);
			this.crtColumn(6);
		}
		if (A == 3) {
			this.crtColumn(4);
			this.crtColumn(5);
			this.crtColumn(6);
		}
		if (A == 2) {
			this.crtColumn(3);
			this.crtColumn(4);
			this.crtColumn(5);
			this.crtColumn(6);
		}
		if (A == 1) {
			this.crtColumn(2);
			this.crtColumn(3);
			this.crtColumn(4);
			this.crtColumn(5);
			this.crtColumn(6);
		}
		this.adjustWidth();
		this.enableDDM();
		this.saveSetting();
		this.adjustView();
		break;
	  case 5:
		if (A == 5) {
			return;
		}
		if (A == 6) {
			this.delColumn(6, 5);
		}
		if (A == 4) {
			this.crtColumn(5);
		}
		if (A == 3) {
			this.crtColumn(4);
			this.crtColumn(5);
		}
		if (A == 2) {
			this.crtColumn(3);
			this.crtColumn(4);
			this.crtColumn(5);
		}
		if (A == 1) {
			this.crtColumn(2);
			this.crtColumn(3);
			this.crtColumn(4);
			this.crtColumn(5);
		}
		this.adjustWidth();
		this.enableDDM();
		this.saveSetting();
		this.adjustView();
		break;
	  case 4:
		if (A == 4) {
			return;
		}
		if (A == 6) {
			this.delColumn(6, 4);
			this.delColumn(5, 4);
		}
		if (A == 5) {
			this.delColumn(5, 4);
		}
		if (A == 3) {
			this.crtColumn(4);
		}
		if (A == 2) {
			this.crtColumn(3);
			this.crtColumn(4);
		}
		if (A == 1) {
			this.crtColumn(2);
			this.crtColumn(3);
			this.crtColumn(4);
		}
		this.adjustWidth();
		this.enableDDM();
		this.saveSetting();
		this.adjustView();
		break;
	  case 2:
		if (A == 2) {
			return;
		}
		if (A == 6) {
			this.delColumn(6, 2);
			this.delColumn(5, 2);
			this.delColumn(4, 2);
			this.delColumn(3, 2);
		}
		if (A == 5) {
			this.delColumn(5, 2);
			this.delColumn(4, 2);
			this.delColumn(3, 2);
		}
		if (A == 4) {
			this.delColumn(4, 2);
			this.delColumn(3, 2);
		}
		if (A == 3) {
			this.delColumn(3, 2);
		}
		if (A == 1) {
			this.crtColumn(2);
		}
		this.adjustWidth();
		this.enableDDM();
		this.saveSetting();
		this.adjustView();
		break;
	  case 1:
		if (A == 1) {
			return;
		}
		if (A == 6) {
			this.delColumn(6, 1);
			this.delColumn(5, 1);
			this.delColumn(4, 1);
			this.delColumn(3, 1);
			this.delColumn(2, 1);
		}
		if (A == 5) {
			this.delColumn(5, 1);
			this.delColumn(4, 1);
			this.delColumn(3, 1);
			this.delColumn(2, 1);
		}
		if (A == 4) {
			this.delColumn(4, 1);
			this.delColumn(3, 1);
			this.delColumn(2, 1);
		}
		if (A == 3) {
			this.delColumn(3, 1);
			this.delColumn(2, 1);
		}
		if (A == 2) {
			this.delColumn(2, 1);
		}
		this.adjustWidth();
		this.enableDDM();
		this.saveSetting();
		this.adjustView();
		break;
	  default:
		if (A == 3) {
			return;
		}
		if (A == 6) {
			this.delColumn(6, 3);
			this.delColumn(5, 3);
			this.delColumn(4, 3);
		}
		if (A == 5) {
			this.delColumn(5, 3);
			this.delColumn(4, 3);
		}
		if (A == 4) {
			this.delColumn(4, 3);
		}
		if (A == 2) {
			this.crtColumn(3);
		}
		if (A == 1) {
			this.crtColumn(2);
			this.crtColumn(3);
		}
		this.adjustWidth();
		this.enableDDM();
		this.saveSetting();
		this.adjustView();
		break;
	}
}, setTheme:function (B) {
	var A = Dom.get("demo_theme");
	if (A) {
		A.href = "css/demo_theme" + B + ".css";
	}
}, showInfo:function (A) {
	if (isIE) {
		window.status = "[模块队列]" + A;
	} else {
		if (isGecko) {
			console.log("[模块队列]" + A);
		}
	}
}, setCookie:function (cookieId, value) {
	var key = cookieId + "=";
	if (document.cookie.length > 0) {
		var index = document.cookie.indexOf(key);
		if (index != -1) {
			index += key.length;
			var end = document.cookie.indexOf(";", index);
			if (end == -1) {
				end = document.cookie.length;
			}
			var oldCookie = document.cookie.substring(index, end);
			document.cookie = document.cookie.replace(oldCookie,'');
			document.cookie = cookieId + "=" + escape(value) + ";" + document.cookie;
		} else {
			document.cookie = cookieId + "=" + escape(value) + ";";
		}
	} else {
		document.cookie = cookieId + "=" + escape(value) + ";";
	}	
}, getCookie:function (cookieId) {
	if(!cookieId)
		cookieId = PanelInfo.id+"_modules";
		
	var key = cookieId + "=";
	if (document.cookie.length > 0) {
		var index = document.cookie.indexOf(key);
		if (index != -1) {
			index += key.length;
			var end = document.cookie.indexOf(";", index);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(index, end));
		} else {
			return null;
		}
	} else {
		return null;
	}
},fillContent:function (){
	for(var i = 0; i <  this.modules.length; i++){
		var module = this.modules[i];
		var moduleContainer = Dom.get(module + "_content");
		var moduleInfo = PanelInfo.rssInfo[module];
		if(moduleInfo.url){
			moduleContainer.contentType = 'remote';
			moduleContainer.innerHtml="<img src='"+Global.contextPath+"/themes/default/portlet/img/ico_loading.gif'/>loading page info...";
			var contentPage = new Ajax.Request(
				moduleInfo.url,
				{
					onComplete: function(response){moduleContainer.innerHTML = response.responseText;},
					onException: function(request, e){moduleContainer.innerText = '加载出错：'+e.message;}
				}
			);
		}else{
			moduleContainer.contentType = 'local';
			var contentString = moduleInfo.content;
			if(contentString != null){
				var contents = contentString.split('|');
				for(var j = 0; j < contents.length; j++){
					var content = document.getElementById(contents[j]);
					if(content.style.display == 'none')
						content.style.display = 'inline';
					moduleContainer.appendChild(content);
				}
			}
		}		
	}
},addCell : function(id, title, content, url, columnIndex, index, autoRefresh){
	eval("PanelInfo.rssInfo." + id + "= {title : '" + title + "', content : '" + content +  "', url:'" + url + "'}");
	var moduleInfo = this.getCookie(PanelInfo.id);
	if(moduleInfo == null)
		moduleInfo = PanelInfo.moduleInfo;
	if(moduleInfo == null){
		moduleInfo = id;
	}else{
		var columns = moduleInfo.split('|');
		if(columnIndex <= 0){
			moduleInfo = id + '|' + moduleInfo;
		}else if(columnIndex >= columns.length){
			moduleInfo = moduleInfo + '|' + id;
		}else{
			var column = columns[columnIndex];
		 	var modules = column.split(',');
		 	if(index <= 0){
				columns[columnIndex] = id + ',' + columns[columnIndex];
			}else if(index >= modules.length){
				columns[columnIndex] = columns[columnIndex] + ',' + id;
			} else{
				var newColumn = '';
				var i = 0;
				var exist = false;
				while(i < modules.length){
					if(index != i || exist){
						newColumn = newColumn + modules[i];
						i++;
					}
					else if(!exist){
						newColumn  = newColumn + id;
						exist = true;
					}
						
					if(i < modules.length)
						newColumn = newColumn + ',';
				}
				columns[columnIndex] = newColumn;			
			}
			moduleInfo = '';
			for(i = 0; i < columns.length; i++){
				moduleInfo = moduleInfo + columns[i];
				if(i < columns.length - 1)
					moduleInfo = moduleInfo + '|';
			}			
		}		
	}
	if(autoRefresh)
		this.init(moduleInfo);
	DDModules.refresh = true;
	DDModules.aT();
},removeCell : function(id,autoRefresh){
	var moduleInfo = this.getCookie(PanelInfo.id);
	if(moduleInfo == null)
		moduleInfo = PanelInfo.moduleInfo;
	if(moduleInfo != null)
		moduleInfo = moduleInfo.replace(id, '');
	if(autoRefresh)
		this.init(moduleInfo);
	DDModules.refresh = true;
	DDModules.aT();
	return true;
},clear : function(){
	var recycleBin = document.getElementById("recycleBin");
	if(recycleBin == null){
		recycleBin = document.createElement("div");
		recycleBin.style.display = 'none';
		recycleBin.id = "recycleBin";
		document.body.appendChild(recycleBin);
	}
		
	if(this.modules == null)
		return;	
	
	for(var i = 0; i <  this.modules.length; i++){
		var module = this.modules[i];
		var moduleContainer = Dom.get(module + "_content");
		var moduleInfo = PanelInfo.rssInfo[module];
		if(moduleContainer.contentType == 'local'){
			var contentString = moduleInfo.content;
			if(contentString != null){
				var contents = contentString.split('|');
				for(var j = 0; j < contents.length; j++){
					var content = document.getElementById(contents[j]);
					recycleBin.appendChild(content);
				}
			}
		}
	}	
}
};
String.prototype.ellipse = function (B) {
	var D = 0;
	for (var A = 0, E = this.length; A < E; A++) {
		var C = this.charCodeAt(A);
		if (C < 256 || (C >= 65377 && C <= 65439)) {
			D += 1;
		} else {
			D += 2;
		}
		if (D > B) {
			return this.substr(0, A - 3) + "...";
		}
	}
	return this;
};
